/**************************************************************************************************************
 * @copyright Copyright (c) 2015—2024 Uniorange Internet Design Co., LTD. All rights reserved.
 * @author Uniorange Internet Design Co., LTD.
 *
 * This file is part of the App project. Copyright © Uniorange Internet Design Co., Ltd. All Rights Reserved.
 * Unauthorized copying of this file, via any medium is strictly prohibited Proprietary and confidential.
 *
 **************************************************************************************************************/
package com.jinyu.core.codec;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jinyu.core.protocol.Header;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * @author 邱金波
 * @version V0.1
 * @className Encoder
 * @description 编码器
 * @date 2024/09/19 15:16
 */
public class Encoder extends MessageToByteEncoder<Object> {
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Object o, ByteBuf byteBuf) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        Header header = (Header)o;
        byte[] bytes = objectMapper.writeValueAsBytes(o);
        byteBuf.writeInt(header.getType());
        byteBuf.writeBytes(bytes);
    }
}
